import { createRouter, createWebHistory } from 'vue-router'
import { isLoggedIn } from '@/utils/auth'
import { ElMessage } from 'element-plus'

// 路由组件
import Login from '@/views/Login.vue'
import Dashboard from '@/views/Dashboard.vue'
import AuthorList from '@/views/author/AuthorList.vue'
import AuthorForm from '@/views/author/AuthorForm.vue'
import BookList from '@/views/book/BookList.vue'
import BookForm from '@/views/book/BookForm.vue'
import CategoryList from '@/views/category/CategoryList.vue'
import CategoryForm from '@/views/category/CategoryForm.vue'
import PublisherList from '@/views/publisher/PublisherList.vue'
import PublisherForm from '@/views/publisher/PublisherForm.vue'
import BorrowList from '@/views/borrow/BorrowList.vue'
import BorrowForm from '@/views/borrow/BorrowForm.vue'
import UserList from '@/views/user/UserList.vue'
import UserForm from '@/views/user/UserForm.vue'
import LogList from '@/views/log/LogList.vue'
import Test from '@/views/Test.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '用户登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    name: 'Dashboard',
    component: Dashboard,
    meta: {
      title: '系统首页',
      requiresAuth: true
    }
  },
  {
    path: '/author',
    name: 'AuthorList',
    component: AuthorList,
    meta: {
      title: '作者管理',
      requiresAuth: true
    }
  },
  {
    path: '/author/add',
    name: 'AuthorAdd',
    component: AuthorForm,
    meta: {
      title: '添加作者',
      requiresAuth: true
    }
  },
  {
    path: '/author/edit/:id',
    name: 'AuthorEdit',
    component: AuthorForm,
    meta: {
      title: '编辑作者',
      requiresAuth: true
    }
  },
  // 图书管理模块
  {
    path: '/book',
    name: 'BookList',
    component: BookList,
    meta: {
      title: '图书管理',
      requiresAuth: true
    }
  },
  {
    path: '/book/add',
    name: 'BookAdd',
    component: BookForm,
    meta: {
      title: '添加图书',
      requiresAuth: true
    }
  },
  {
    path: '/book/edit/:id',
    name: 'BookEdit',
    component: BookForm,
    meta: {
      title: '编辑图书',
      requiresAuth: true
    }
  },
  // 分类管理模块
  {
    path: '/category',
    name: 'CategoryList',
    component: CategoryList,
    meta: {
      title: '分类管理',
      requiresAuth: true
    }
  },
  {
    path: '/category/add',
    name: 'CategoryAdd',
    component: CategoryForm,
    meta: {
      title: '添加分类',
      requiresAuth: true
    }
  },
  {
    path: '/category/edit/:id',
    name: 'CategoryEdit',
    component: CategoryForm,
    meta: {
      title: '编辑分类',
      requiresAuth: true
    }
  },
  // 出版社管理模块
  {
    path: '/publisher',
    name: 'PublisherList',
    component: PublisherList,
    meta: {
      title: '出版社管理',
      requiresAuth: true
    }
  },
  {
    path: '/publisher/add',
    name: 'PublisherAdd',
    component: PublisherForm,
    meta: {
      title: '添加出版社',
      requiresAuth: true
    }
  },
  {
    path: '/publisher/edit/:id',
    name: 'PublisherEdit',
    component: PublisherForm,
    meta: {
      title: '编辑出版社',
      requiresAuth: true
    }
  },
  // 借阅管理模块
  {
    path: '/borrow',
    name: 'BorrowList',
    component: BorrowList,
    meta: {
      title: '借阅管理',
      requiresAuth: true
    }
  },
  {
    path: '/borrow/add',
    name: 'BorrowAdd',
    component: BorrowForm,
    meta: {
      title: '添加借阅',
      requiresAuth: true
    }
  },
  {
    path: '/borrow/edit/:id',
    name: 'BorrowEdit',
    component: BorrowForm,
    meta: {
      title: '编辑借阅',
      requiresAuth: true
    }
  },
  // 用户管理模块
  {
    path: '/user',
    name: 'UserList',
    component: UserList,
    meta: {
      title: '用户管理',
      requiresAuth: true
    }
  },
  {
    path: '/user/add',
    name: 'UserAdd',
    component: UserForm,
    meta: {
      title: '添加用户',
      requiresAuth: true
    }
  },
  {
    path: '/user/edit/:id',
    name: 'UserEdit',
    component: UserForm,
    meta: {
      title: '编辑用户',
      requiresAuth: true
    }
  },
  {
    path: '/log',
    name: 'LogList',
    component: LogList,
    meta: {
      title: '操作日志',
      requiresAuth: true
    }
  },
  {
    path: '/setting',
    redirect: '/',
    meta: { requiresAuth: true }
  },
  {
    path: '/test',
    name: 'Test',
    component: Test,
    meta: {
      title: '连接测试',
      requiresAuth: false
    }
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 图书管理系统`
  } else {
    document.title = '图书管理系统'
  }

  // 检查是否需要认证
  if (to.meta.requiresAuth !== false) {
    // 需要认证的页面
    if (!isLoggedIn()) {
      ElMessage.warning('请先登录')
      next('/login')
      return
    }
  } else {
    // 不需要认证的页面（如登录页）
    if (to.path === '/login' && isLoggedIn()) {
      // 已登录用户访问登录页，重定向到首页
      next('/')
      return
    }
  }

  next()
})

export default router
